package com.pk.platform.business.flow.dao.impl;

import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.impl.GenericDaoImpl;
import com.pk.platform.business.flow.dao.IFlowExecuteDao;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.filter.SystemContext;

public class FlowExecuteDaoImpl extends GenericDaoImpl implements
		IFlowExecuteDao {
	
	public List<Map<String, Object>> queryFlowHistory(String tableName, String docId)
	{
		String sql = "select fh.*, u.username from p_flowhistory fh, p_user u where fh.auditorId = u.id and fh.tableName = ?";
		sql += " and fh.docId = ? order by fh.auditTime";
		return super.getSjt().queryForList(sql, new Object[]{tableName, docId});
	}
	
	public List<Map<String, Object>> queryFlowStatusHistory(String tableName, String docId, boolean includeReturn)
	{
		String sql = "select fsh.* from p_flowstatushistory fsh where fsh.tableName = ? and fsh.docId = ?";
		if(!includeReturn)
		{
			sql += " and fsh.auditType <> '" + Constant.RETURN + "'";
		}
		sql += " order by fsh.nodeTime desc";
		return super.getSjt().queryForList(sql, new Object[]{tableName, docId});
	}
	
	public void delFlowHistory(String tableName, String docId)
	{
		String sql = "delete from p_flowhistory where tableName = ? and docId = ?";
		this.getSjt().update(sql, new Object[]{tableName, docId});
	}

	public void delFlowStatusHistory(String tableName, String docId)
	{
		String sql = "delete from p_flowstatushistory where tableName = ? and docId = ?";
		this.getSjt().update(sql, new Object[]{tableName, docId});
	}
	
	public int queryThroughCount(String tableName, String docId, String nodeId, int version)
	{
		String sql = "select count(fh.id) from p_flowhistory fh where fh.tableName = ? and fh.docId = ?";
		sql += " and fh.currentNodeId = ? and fh.version = ? and fh.auditType <> ?";
		return this.getSjt().queryForInt(sql, new Object[]{tableName, docId, nodeId, version, Constant.RETURN});
	}
	
	public Map<String, Object> queryCurrentStatus(String tableName, String docId, String nodeId, int version)
	{
		String sql = "select fsh.* from p_flowstatushistory fsh where fsh.tableName = ? and fsh.docId = ?";
		sql += " and fsh.nextNodeId = ? and fsh.version = ? order by fsh.nodeTime desc";
		List<Map<String, Object>> list = this.getSjt().queryForList(sql, new Object[]{tableName, docId, nodeId, version});
		if(list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public boolean hasSubmit(String tableName, String docId, String nodeId, int version)
	{
		String sql = "select count(fh.id) from p_flowhistory fh where fh.tableName = ? and fh.docId = ?";
		sql += " and fh.currentNodeId = ? and fh.version = ? and fh.auditorId = ?";
		User user = SystemContext.getUser();
		int count = this.getSjt().queryForInt(sql, new Object[]{tableName, docId, nodeId, version, user.getId()});
		if(count > 0)
			return true;
		return false;
	}
	
	public List<Map<String, Object>> queryDistinctFlowStatusSubmit(String tableName, String docId)
	{
		String sql = "select distinct fsh.currentNodeId, fsh.nextNodeId from p_flowstatushistory fsh";
		sql += " where fsh.tableName = ? and fsh.docId = ? and fsh.auditType = ?";
		return super.getSjt().queryForList(sql, new Object[]{tableName, docId, Constant.SUBMIT});
	}

}
